/*
 * @filename LogUtils.java
 * @author barry
 * @version 0.0.1
 * @date 2019年12月25日
 */
package com.bnzj.core.util;

import org.slf4j.Logger;

/**
 * @author barry
 * @date 2019-12-25
 */
public class LogUtils {

    private static String buildLogInfo(String msg, Throwable e, int row) {
        StringBuilder stringBuilder = new StringBuilder(msg);
        StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
        if(stacks.length > 3) {
            stringBuilder.append(FbConstant.LINE_SEPARATOR).append("---").append(stacks[3].toString());
        }
        stringBuilder.append(FbConstant.LINE_SEPARATOR).append("---").append(e);
        StackTraceElement[] stackTraceElements = e.getStackTrace();
        for(int i = 0; i < stackTraceElements.length && i < row; i++) {
            stringBuilder.append(FbConstant.LINE_SEPARATOR).append("---").append(stackTraceElements[i]);
        }
        stringBuilder.append(FbConstant.LINE_SEPARATOR);
        return stringBuilder.toString();
    }
    
    public static void error(Logger log, String msg, Throwable e, int row) {
        if(log.isErrorEnabled()) {
            log.error(buildLogInfo(msg, e, row));
        }
    }
    
    public static void warn(Logger log, String msg, Throwable e, int row) {
        if(log.isWarnEnabled()) {
            log.warn(buildLogInfo(msg, e, row));
        }
    }
    
    public static void info(Logger log, String msg, Throwable e, int row) {
        if(log.isInfoEnabled()) {
            log.info(buildLogInfo(msg, e, row));
        }
    }
    
    public static void debug(Logger log, String msg, Throwable e, int row) {
        if(log.isDebugEnabled()) {
            log.debug(buildLogInfo(msg, e, row));
        }
    }
    
    public static void trace(Logger log, String msg, Throwable e, int row) {
        if(log.isTraceEnabled()) {
            log.trace(buildLogInfo(msg, e, row));
        }
    }
}
